package com.ilop.sthome.ui.adapter.scene;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.common.utils.LiveDataBus;
import com.ilop.sthome.common.CommonId;
import com.ilop.sthome.data.enums.SmartDevice;
import com.ilop.sthome.data.greenDao.AutomationBean;
import com.ilop.sthome.data.greenDao.DeviceInfoBean;
import com.ilop.sthome.utils.CoderUtils;
import com.siterwell.familywellplus.R;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author skygge
 * @date 2019-11-08.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：场景详情页面
 */
public class SceneDetailAdapter extends RecyclerView.Adapter<SceneDetailAdapter.ItemHolder> {

    private Context mContext;
    private int mSceneId;
    private List<AutomationBean> mList;

    public SceneDetailAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setList(List<AutomationBean> mList) {
        this.mList = mList;
        notifyDataSetChanged();
    }

    public void setSceneId(int mSceneId) {
        this.mSceneId = mSceneId;
    }

    @NonNull
    @Override
    public ItemHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_scene_detail, viewGroup, false);
        return new ItemHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemHolder itemHolder, int i) {
        AutomationBean bean = mList.get(i);

        List<DeviceInfoBean> inputList = bean.getInputList();
        List<DeviceInfoBean> outputList = bean.getOutputList();

        int inputSize = inputList.size();
        SmartDevice smartDevice = SmartDevice.getType(inputList.get(0).getDevice_type());
        if (smartDevice == SmartDevice.EE_SIMULATE_CLICK){
            itemHolder.mClick.setVisibility(View.VISIBLE);
            itemHolder.input1.setVisibility(View.GONE);
            itemHolder.input2.setVisibility(View.GONE);
        }else {
            if (inputSize == 1){
                itemHolder.mClick.setVisibility(View.GONE);
                itemHolder.input1.setVisibility(View.VISIBLE);
                itemHolder.input2.setVisibility(View.GONE);
                itemHolder.input1.setImageResource(smartDevice.getDrawableResId());
            }else {
                itemHolder.mClick.setVisibility(View.GONE);
                itemHolder.input1.setVisibility(View.VISIBLE);
                itemHolder.input2.setVisibility(View.VISIBLE);
                itemHolder.input1.setImageResource(smartDevice.getDrawableResId());
            }
        }

        int outputSize = outputList.size();
        switch (outputSize){
            case 1:
                itemHolder.output1.setImageResource(SmartDevice.getType(outputList.get(0).getDevice_type()).getDrawableResId());
                itemHolder.output1.setVisibility(View.VISIBLE);
                itemHolder.output2.setVisibility(View.GONE);
                itemHolder.output3.setVisibility(View.GONE);
                itemHolder.output4.setVisibility(View.GONE);
                break;
            case 2:
                itemHolder.output1.setImageResource(SmartDevice.getType(outputList.get(0).getDevice_type()).getDrawableResId());
                itemHolder.output2.setImageResource(SmartDevice.getType(outputList.get(1).getDevice_type()).getDrawableResId());
                itemHolder.output1.setVisibility(View.VISIBLE);
                itemHolder.output2.setVisibility(View.VISIBLE);
                itemHolder.output3.setVisibility(View.GONE);
                itemHolder.output4.setVisibility(View.GONE);
                break;
            case 3:
                itemHolder.output1.setImageResource(SmartDevice.getType(outputList.get(0).getDevice_type()).getDrawableResId());
                itemHolder.output2.setImageResource(SmartDevice.getType(outputList.get(1).getDevice_type()).getDrawableResId());
                itemHolder.output3.setImageResource(SmartDevice.getType(outputList.get(2).getDevice_type()).getDrawableResId());
                itemHolder.output1.setVisibility(View.VISIBLE);
                itemHolder.output2.setVisibility(View.VISIBLE);
                itemHolder.output3.setVisibility(View.VISIBLE);
                itemHolder.output4.setVisibility(View.GONE);
                break;
            default:
                itemHolder.output1.setImageResource(SmartDevice.getType(outputList.get(0).getDevice_type()).getDrawableResId());
                itemHolder.output2.setImageResource(SmartDevice.getType(outputList.get(1).getDevice_type()).getDrawableResId());
                itemHolder.output3.setImageResource(SmartDevice.getType(outputList.get(2).getDevice_type()).getDrawableResId());
                itemHolder.output1.setVisibility(View.VISIBLE);
                itemHolder.output2.setVisibility(View.VISIBLE);
                itemHolder.output3.setVisibility(View.VISIBLE);
                itemHolder.output4.setVisibility(View.VISIBLE);
                break;
        }

        List<String> name = new ArrayList<>();
        if ("TIMER".equals(inputList.get(0).getDevice_type())){
            DeviceInfoBean device = inputList.get(0);
            int hour = Integer.parseInt(device.getDevice_status().substring(4, 6), 16);
            int min = Integer.parseInt(device.getDevice_status().substring(6), 16);
            String time = CoderUtils.setCodeSupplement1Byte(String.valueOf(hour))
                    + ":"+ CoderUtils.setCodeSupplement1Byte(String.valueOf(min)) ;
            name.add(time);
        }else {
            for (DeviceInfoBean device : inputList) {
                name.add(mContext.getString(SmartDevice.getType(device.getDevice_type()).getTypeStrId()));
            }
        }
        for (DeviceInfoBean outDevice: outputList) {
            name.add(mContext.getString(SmartDevice.getType(outDevice.getDevice_type()).getTypeStrId()));
        }
        String mAutoName = bean.getMid() + "\t\t"+ StringUtils.join(name.toArray(),"\t-\t");
        itemHolder.mTitle.setText(mAutoName);
        itemHolder.mTitle.setEllipsize(TextUtils.TruncateAt.MARQUEE);
        itemHolder.mTitle.setSingleLine(true);
        itemHolder.mTitle.setSelected(true);
        itemHolder.mTitle.setFocusable(true);
        itemHolder.mTitle.setFocusableInTouchMode(true);

        if ("00".equals(bean.getReserve())) {
            itemHolder.mCorrect.setVisibility(View.VISIBLE);
            itemHolder.mTitle.setTextColor(ContextCompat.getColor(mContext, R.color.text_title));
            if ("7FFF".equals(bean.getSceneId())) {
                itemHolder.mCorrect.setEnabled(false);
                itemHolder.mCorrect.setImageResource(R.mipmap.check_empty);
            } else if ("0000".equals(bean.getSceneId())) {
                itemHolder.mCorrect.setEnabled(true);
                itemHolder.mCorrect.setImageResource(R.mipmap.check_unselected);
            } else {
                List<String> sceneList = CoderUtils.getSceneList(bean.getSceneId());
                if (sceneList.contains(String.valueOf(mSceneId))) {
                    itemHolder.mCorrect.setEnabled(true);
                    itemHolder.mCorrect.setImageResource(R.mipmap.check_selected);
                } else {
                    itemHolder.mCorrect.setEnabled(true);
                    itemHolder.mCorrect.setImageResource(R.mipmap.check_unselected);
                }
            }
            itemHolder.itemView.setBackgroundResource(R.drawable.item_click);
        }else {
            List<String> sceneList = CoderUtils.getSceneList(bean.getSceneId());
            if (sceneList.contains(String.valueOf(mSceneId))) {
                itemHolder.setVisibility(true);
                itemHolder.mCorrect.setEnabled(false);
                itemHolder.mCorrect.setImageResource(R.mipmap.check_empty);
                itemHolder.mTitle.setTextColor(ContextCompat.getColor(mContext, R.color.text_normal));
                itemHolder.itemView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.line_bg));
            } else {
                itemHolder.setVisibility(false);
            }
        }

        itemHolder.mCorrect.setOnClickListener(view -> LiveDataBus.get().with(CommonId.CHANGE_SCENE_AUTO).setValue(bean));

        itemHolder.mClick.setOnClickListener(view -> LiveDataBus.get().with(CommonId.AUTO_CLICK_TO).setValue(bean));

        itemHolder.itemView.setOnClickListener(v -> LiveDataBus.get().with(CommonId.AUTO_ITEM_CLICK).setValue(bean));

        itemHolder.itemView.setOnLongClickListener(v -> {
            if ("00".equals(bean.getReserve())) {
                LiveDataBus.get().with(CommonId.AUTO_ITEM_LONG_CLICK).setValue(bean);
            }
            return false;
        });
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    static class ItemHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.auto_name)
        TextView mTitle;
        @BindView(R.id.input1)
        ImageView input1;
        @BindView(R.id.input2)
        ImageView input2;
        @BindView(R.id.output1)
        ImageView output1;
        @BindView(R.id.output2)
        ImageView output2;
        @BindView(R.id.output3)
        ImageView output3;
        @BindView(R.id.output4)
        ImageView output4;
        @BindView(R.id.auto_done)
        ImageView mCorrect;
        @BindView(R.id.bt_auto_click)
        Button mClick;
        @BindView(R.id.auto_right)
        RelativeLayout mRightClick;
        public ItemHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        private void setVisibility(boolean visible) {
            RecyclerView.LayoutParams param = (RecyclerView.LayoutParams) itemView.getLayoutParams();
            if (visible) {
                param.height = LinearLayout.LayoutParams.WRAP_CONTENT; // 根据具体需求场景设置
                param.width = LinearLayout.LayoutParams.MATCH_PARENT;
                itemView.setVisibility(View.VISIBLE);
            } else {
                itemView.setVisibility(View.GONE);
                param.height = 1;
                param.width = 0;
            }
            itemView.setLayoutParams(param);
        }
    }
}
